//
//  MVMyInfoViewController.m
//  Gunz
//
//  Created by Joggy on 16/1/25.
//  Copyright © 2016年 NUPT. All rights reserved.
//

#import "MVMyInfoViewController.h"
#import "MMTableViewCell.h"

@interface MVMyInfoViewController ()

@end

@implementation MVMyInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self prepareForNav];
    [self prepareForLayout];
//    myInfo = [[NSArray alloc] initWithObjects:@"昵称", @"性别", @"简介", @"实名制", @"我的声誉", @"我的智币", @"我的余额", @"手机", @"邮箱", nil];
    infoTitle = @[
                  @[],
                  @[@"昵称", @"性别", @"简介", @"实名制"],
                  @[@"我的声誉", @"我的智币", @"我的余额"],
                  @[@"手机", @"邮箱"]
                  ];
    userInfo = @[
                 @[@""],
                 @[@"Qiuny", @"男", @"一个帅气的众客", @"未实名制"],
                 @[@"100", @"0", @"0"],
                 @[@"17095354949", @"qiu2458@163.com"],
                 ];
}

- (void)prepareForNav {
    [super prepareForNav];
    //导航左按键
    UIImage *image = [UIImage imageNamed:@"all_back"];
    leftBarButton = [UIButton buttonWithType:UIButtonTypeSystem];
    leftBarButton.frame = CGRectMake(0, 0, image.size.width, image.size.height);
    leftBarButton.layer.position = CGPointMake(22, 22);
    [leftBarButton setImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [leftBarButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBarButton];
}

- (void)prepareForLayout {
    [super prepareForLayout];
    self.title = @"关于我";
    //tableview
    tb = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight - 64) style:UITableViewStylePlain];
    tb.delegate = self;
    tb.dataSource = self;
    tb.backgroundColor = [UIColor clearColor];
    tb.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 10)];
    tb.showsVerticalScrollIndicator = false;
    tb.separatorStyle = UITableViewCellSelectionStyleBlue;
    tb.separatorColor = [UIColor colorWithRed:0.85 green:0.85 blue:0.85 alpha:1];
    [tb registerClass:[UITableViewCell class] forCellReuseIdentifier:@"MMCell1"];
    [tb registerClass:[MMTableViewCell class] forCellReuseIdentifier:@"MMCell2"];
    [self.view addSubview:tb];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma 事件方法

-(void)takePicture {
    NSLog(@"拍照");
    [action dismiss];
}

-(void)getFromPhotoAlbum {
    getPhotos = [[QYPhotos alloc] init];
    [getPhotos setBarItemColor:CUSTOM_GREEN];
    [getPhotos setBarTintColor:[UIColor whiteColor]];
    [getPhotos setSelectNumber:1];
    getPhotos.delegate = self;
    [getPhotos showPhotos:self];
    [action dismiss];
}

-(void)cancelAction {
    NSLog(@"取消");
    [action dismiss];
}

-(void)back {
    [self.navigationController popViewControllerAnimated:true];
}

#pragma 代理方法

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:true];
    if(indexPath.section == 0) {
        action = [[MVActionSheet alloc] initWithFrame:CGRectMake(0, screenHeight, screenWidth, MVASbgHeight)];
        [action.registerByPhoneButton addTarget:self action:@selector(takePicture) forControlEvents:UIControlEventTouchUpInside];
        [action.registerByEmailButton addTarget:self action:@selector(getFromPhotoAlbum) forControlEvents:UIControlEventTouchUpInside];
        [action.cancelButton addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
        [action show];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.section == 0) {
        return 88;
    }
    else {
        return 47;
    }
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch(section) {
        case 0:
            return 1;
            break;
        case 1:
            return 4;
            break;
        case 2:
            return 3;
            break;
        case 3:
            return 2;
            break;
        default:
            return 0;
            break;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 6;
}

-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 10)];
    v.backgroundColor = [UIColor clearColor];
    return v;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    if(indexPath.section == 0 && indexPath.row == 0) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MMCell1"];
        tableView.rowHeight = 88;
        userIcon = [[MVUserIcon alloc] init];
        [cell.contentView addSubview:userIcon];
        cell.backgroundColor = [UIColor colorWithRed:0.9765 green:0.9765 blue:0.9843 alpha:1];
        return cell;
    }
    else {
        MMTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MMCell2"];
        NSArray *arr = infoTitle[indexPath.section];
        cell.textLabel.text = arr[indexPath.row];
        cell.textLabel.alpha = 0.7;
        NSArray *arr1 = userInfo[indexPath.section];
        cell.infoLabel.text = arr1[indexPath.row];
        cell.backgroundColor = [UIColor colorWithRed:0.9765 green:0.9765 blue:0.9843 alpha:1];
        return cell;
    }
}

-(void)selectedPhotos:(NSArray<UIImage *> *)photos {
    NSLog(@"协议");
    if(userIcon != nil && photos.count > 0) {
        userIcon.icon.image = photos[0];
    }
}

@end
